<template>
  <e-table
    ref="eTable"
    :tableData="fieldData"
    :columns="fieldColumns"
    :loading="loading"
    :pageObj="null"
  >
    <el-table-column label="字段" slot="field" align="center">
      <template slot-scope="scope">
        <el-select clearable v-model="scope.row.field">
          <el-option v-for="(item,index) in repeatData" :key="index" :value="item" :label="item" />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column label="类型" slot="type" align="center">
      <template slot-scope="scope">
        <el-select clearable v-model="scope.row.type">
          <el-option :value="0" label="数字" />
          <el-option :value="1" label="字符串" />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column label="字段显示名" slot="showName" align="center">
      <template slot-scope="scope">
        <el-input v-model="scope.row.showName" />
      </template>
    </el-table-column>
    <el-table-column label="宽度" slot="width" align="center">
      <template slot-scope="scope">
        <el-input type="number" v-model="scope.row.width" />
      </template>
    </el-table-column>
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleDelete(scope.row)">
              <a class="red">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
import { openNotification } from '@/utils/util'

export default {
  data() {
    return {
      loading: false,
      fieldData: [],
      fieldColumns: [{ slot: 'field' }, { slot: 'showName' }, { slot: 'type' }, { slot: 'width' }, { slot: 'action' }]
    }
  },
  watch: {
    tableData: {
      handler(n, o) {
        // if (n !== o) {
        this.fieldData = [...n]
        // }
      },
      deep: true
    }
  },
  props: {
    tableData: {
      type: Array,
      default: []
    },
    repeatData: {
      type: Array,
      default: []
    }
  },
  methods: {
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const index = _this.fieldData.findIndex(v => v.xmSpecialId === row.xmSpecialId)
          _this.fieldData.splice(index, 1)
          openNotification('success', '删除成功')
          _this.$emit('tableUpdate', _this.fieldData)
        }
      })
    }
  }
}
</script>
